<%@page import="java.sql.ResultSet"%>
<%@page import="DAO.DAO_Student"%>
<%@page import="Entity.Student"%>
<%
    DAO_Student dao = new DAO_Student();
    Student student = null;
    String name = "";
    String sage = "";
    int sex = 0;
    String hometown = "";
    
    if (request.getParameter("submit") != null) {
        boolean ok = true;
        name = request.getParameter("name");
        if (name == null || name.isEmpty()) {
            out.println("<p>Name cannot be empty</p>");
            ok = false;
        } else {
            name = new String(name.getBytes("ISO8859_1"), "UTF-8");
            if (name.length() > 50) {
                out.println("<p>Name must not be greater than 50 characters</p>");
                ok = false;
            }                    
        }

        sage = request.getParameter("age");
        int age = 0;
        if (sage == null || sage.isEmpty()) {
            out.println("<p>Age cannot be empty</p>");
            ok = false;
        } else {
            try {
                age = Integer.parseInt(sage);
                if (age <= 0 || age >= 200) {
                    out.println("<p>Age must be a positive number between 1 to 199</p>");
                    ok = false;
                }
            } catch (Exception ex) {
                out.println("<p>Age must be a positive number between 1 to 199</p>");
                ok = false;
            }
        }

        String ssex = request.getParameter("sex");
        sex = 0;
        if (ssex == null || ssex.isEmpty()) {
            out.println("<p>Sex cannot be empty</p>");
            ok = false;
        } else {
            sex = Integer.parseInt(ssex);
        }

        hometown = request.getParameter("hometown");
        if (hometown == null || hometown.isEmpty()) {
            out.println("<p>Hometown cannot be empty</p>");
            ok = false;
        } else {
            hometown = new String(hometown.getBytes("ISO8859_1"), "UTF-8");
            if (hometown.length() > 20) {
                out.println("<p>Hometown must not be greater than 20 characters</p>");
                ok = false;
            }
        }

        if (ok) {
            student = new Student(name, age, sex, hometown);
            dao.addStudent(student);
            response.sendRedirect("Student.jsp");
        }
    }
%>
<h2 class="sub-header">Add New Student</h2>   
<form role="form" method="post">
    <div class="form-group">
        <label>Name:</label>
        <input type="text" name="name" value="<%=name%>" class="form-control" placeholder="Enter fullname">
    </div>

    <div class="form-group">
        <label>Age:</label>
        <input type="text" name="age" value="<%=sage%>" class="form-control" placeholder="Enter age">
    </div>  
    <label>Sex:</label>
    <label class="radio-inline">
        <input type="radio" name="sex" <% if (sex == 0) {
              out.print("checked='true'");
          } %> value="0"> Female
    </label>
    <label class="radio-inline">
        <input type="radio" name="sex" <% if (sex == 1) {
              out.print("checked='true'");
          } %> value="1"> Male
    </label>
    <label class="radio-inline">
        <input type="radio" name="sex" <% if (sex == 2) {
              out.print("checked='true'");
          }%> value="2"> Unknown
    </label>

    <div class="form-group">
        <label>Hometown:</label>
        <input type="text" name="hometown" value="<%=hometown%>" class="form-control" placeholder="Enter hometown">
    </div>      
    <button type="submit" name="submit" class="btn btn-primary btn-lg" value="Log in" style="border-radius: 0"><i class="fa fa-plus-square"></i> Add Student</button>
</form>